﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.Core;
using WarehouseManager.Model;
using WarehouseManager.DataAccess;
using System.Data;
using System.Data.SqlClient;

namespace WarehouseManager.View
{
    public partial class FormNhanvien : Form
    {
        NhanvienAccess nvaccess = new NhanvienAccess();
        DataTable dsnv = new DataTable();
        
        public FormNhanvien()
        {
            InitializeComponent();
        }

        private void FormNhanvien_Load(object sender, EventArgs e)
        {
            dsnv = nvaccess.Laytoanbonhanvien();
            dataGridView1.DataSource = dsnv;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Nhanvien nv = new Nhanvien(txtManhanvien.Text, txtten.Text,cbbGioitinh.Text, txtDiachi.Text, txtSdt.Text, txtEmail.Text);
            if ( nvaccess.themnhanvien(nv) == 0) { MessageBox.Show("Mã nhanvien đã tồn tại !"); }
            else
            {
                MessageBox.Show("Thêm thành công !");
            }
            FormNhanvien_Load(sender, e);
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            DataView dataView = new DataView();
            if (cbbTimkiem.SelectedItem==cbbTimkiem .Items[0])
            {
                dataView = nvaccess.timkiemManv(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
            else
            {
                dataView = nvaccess.timkiemtennhanvien(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtManhanvien.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtten.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            cbbGioitinh.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtDiachi.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtSdt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtEmail.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Nhanvien nv = new Nhanvien(txtManhanvien.Text, txtten.Text, cbbGioitinh.Text, txtDiachi.Text, txtSdt.Text, txtEmail.Text);
            if (nvaccess.suanhanvien(nv) == 0) { MessageBox.Show("Mã nhanvien đã tồn tại !"); }
            else
            {
                MessageBox.Show("sửa thành công !");
            }
            FormNhanvien_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Nhanvien nv = new Nhanvien(txtManhanvien.Text, txtten.Text, cbbGioitinh.Text, txtDiachi.Text, txtSdt.Text, txtEmail.Text);
            if (nvaccess.xoanv(nv) == 0) { MessageBox.Show("Mã nhanvien đã tồn tại !"); }
            else
            {
                MessageBox.Show("xóa thành công !");
            }
            FormNhanvien_Load(sender, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                txtManhanvien.ReadOnly = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtManhanvien.ReadOnly = false;
                btnThem.Enabled = false;
            }
        }
    }
}
